package com.yuyou.fn.portal.service.impl;

import com.github.wens.mybatisplus.examples.Example;
import com.github.wens.mybatisplus.plugins.Page;
import com.pingplusplus.model.Customer;
import com.yuyou.fn.common.exception.BusException;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.CriteriaUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.CustomerBindStudent;
import com.yuyou.fn.educational.entity.Student;
import com.yuyou.fn.educational.service.ICustomerBindStudentService;
import com.yuyou.fn.educational.service.ICustomerUserService;
import com.yuyou.fn.educational.service.IStudentService;
import com.yuyou.fn.platform.entity.CustomerUser;
import com.yuyou.fn.platform.permission.DataPermission;
import com.yuyou.fn.portal.vo.ShoppingCartDetailQueryVo;
import com.yuyou.fn.portal.vo.ShoppingCartSituationVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.stereotype.Service;

import com.yuyou.fn.portal.mapper.ShoppingCartDetailMapper;
import com.yuyou.fn.portal.entity.ShoppingCartDetail;
import com.yuyou.fn.portal.service.IShoppingCartDetailService;
import com.github.wens.framework.service.impl.SuperServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * ShoppingCartDetail 表数据服务层接口实现类
 *
 */
@Service
public class ShoppingCartDetailServiceImpl extends SuperServiceImpl<ShoppingCartDetailMapper, ShoppingCartDetail,String> implements IShoppingCartDetailService {

    @Resource
    private ICustomerUserService customerUserService;

    @Resource
    private ICustomerBindStudentService customerBindStudentService;

    @Resource
    private IStudentService studentService;

    @Override
    public Page<ShoppingCartDetail> findShoppingCartDetailPageByQueryVo(DataPermission dataPermission,ShoppingCartDetailQueryVo shoppingCartDetailQueryVo,int pageNo,int pageSize) {
        if (dataPermission==null || Utils.checkListEmpty(dataPermission.getBusTeamIds())){
            throw  new BusException("无数据权限");
        }
        Example<ShoppingCartDetail> example=ShoppingCartDetail.createExample();
        Example.Criteria criteria=example.createCriteria();
        CriteriaUtils.fillCondition(criteria,shoppingCartDetailQueryVo);
        CriteriaUtils.fillConditionForBusPermission(criteria,dataPermission.getBusTeamIds());
        example.orderBy("busTeamId").orderBy("userId");
        return this.findPageByExample(example,pageNo,pageSize);
    }

    @Override
    public Page<ShoppingCartSituationVo> findShoppingCartSituation(DataPermission dataPermission,ShoppingCartDetailQueryVo shoppingCartDetailQueryVo, int pageNo, int pageSize) {
        Page<ShoppingCartDetail> shoppingCartDetailPage = this.findShoppingCartDetailPageByQueryVo(dataPermission,shoppingCartDetailQueryVo, pageNo, pageSize);

        Page<ShoppingCartSituationVo> shoppingCartSituationVoPage= Utils.newPage(shoppingCartDetailPage);

        List<ShoppingCartDetail> shoppingCartDetails=shoppingCartDetailPage.getRecords();
        if (shoppingCartDetails!=null){
            List<ShoppingCartSituationVo> shoppingCartSituationVoList=new ArrayList<>(shoppingCartDetails.size());
            List<Long> userIds = BeanUtils.toList(shoppingCartDetails, "userId");
            userIds=Utils.removeDuplicate(userIds);
            List<CustomerUser> customerUsers=customerUserService.findByIds(userIds);
            Map<Long, CustomerUser> customerUserMap = BeanUtils.toMap(customerUsers, "userId");

            List<CustomerBindStudent> customerBindStudentList=customerBindStudentService.findStudentsByCustomerUserIds(userIds);
            Map<Object, List<CustomerBindStudent>> customerBindStudentListMap = BeanUtils.toGroup(customerBindStudentList, "userId");
            List<Long> studentIds=BeanUtils.toList(customerBindStudentList,"studentId");
            studentIds=Utils.removeDuplicate(studentIds);
            List<Student> studentList=studentService.findByIds(studentIds,"studentId","name");
            Map<Long,Student> studentMap=BeanUtils.toMap(studentList,"studentId");
            for (ShoppingCartDetail shoppingCartDetail:shoppingCartDetails){
                ShoppingCartSituationVo shoppingCartSituationVo=new ShoppingCartSituationVo();
                BeanUtils.copyProperties(shoppingCartSituationVo,shoppingCartDetail);
                CustomerUser customerUser=customerUserMap.get(shoppingCartDetail.getUserId());
                if (customerUser!=null){
                    shoppingCartSituationVo.setCustomerPhoneNo(customerUser.getPhoneNo());
                }
                List<CustomerBindStudent> customerBindStudents=customerBindStudentListMap.get(shoppingCartDetail.getUserId());
                if (customerBindStudents!=null){
                    for (CustomerBindStudent customerBindStudent:customerBindStudents){
                        Long studentId=customerBindStudent.getStudentId();
                        Student student=studentMap.get(studentId);
                        if(student!=null){
                            shoppingCartSituationVo.setStudentIds((StringUtils.isEmpty(shoppingCartSituationVo.getStudentIds())?"":shoppingCartSituationVo.getStudentIds()+",")+student.getStudentId());
                            shoppingCartSituationVo.setStudentNames((StringUtils.isEmpty(shoppingCartSituationVo.getStudentNames())?"":shoppingCartSituationVo.getStudentNames()+",")+student.getName());
                        }
                    }
                }
                shoppingCartSituationVoList.add(shoppingCartSituationVo);
            }
            shoppingCartSituationVoPage.setRecords(shoppingCartSituationVoList);
        }
        return shoppingCartSituationVoPage;
    }


    @Override
    public SXSSFWorkbook exportShoppingCartSituation(DataPermission dataPermission,ShoppingCartDetailQueryVo shoppingCartDetailQueryVo) {
        Page<ShoppingCartSituationVo> shoppingCartSituationVoPage=this.findShoppingCartSituation(dataPermission,shoppingCartDetailQueryVo,1,50000);
        return shoppingCartSituation2excel(shoppingCartSituationVoPage.getRecords());
    }


    public static SXSSFWorkbook shoppingCartSituation2excel(List<ShoppingCartSituationVo> shoppingCartSituationVoList) {

        SXSSFWorkbook excel = new SXSSFWorkbook();
        SXSSFSheet sheet = excel.createSheet("购物车概况");
        SXSSFRow firstRow = sheet.createRow(0);
        String[] titles = new String[]{ "业务组","期数","年级","科目", "班级名称","绑定学员","登录账号"};
        for (int i = 0; i < titles.length; i++) {
            firstRow.createCell(i).setCellValue(titles[i]);
        }
        int rowNum = 1;

        if (shoppingCartSituationVoList!=null){
            for (ShoppingCartSituationVo shoppingCartSituationVo:shoppingCartSituationVoList){
                int cellNum = 0;

                SXSSFRow row = sheet.createRow(rowNum++);

                SXSSFCell cell = row.createCell(cellNum++);
                cell.setCellValue(StringUtils.defaultString(shoppingCartSituationVo.getBusTeamName()));

                cell = row.createCell(cellNum++);
                cell.setCellValue(StringUtils.defaultString(shoppingCartSituationVo.getPeriodName()));

                cell = row.createCell(cellNum++);
                cell.setCellValue(StringUtils.defaultString(shoppingCartSituationVo.getGradeName()));

                cell = row.createCell(cellNum++);
                cell.setCellValue(StringUtils.defaultString(shoppingCartSituationVo.getSubjectName()));

                cell = row.createCell(cellNum++);
                cell.setCellValue(StringUtils.defaultString(shoppingCartSituationVo.getClassName()));

                cell = row.createCell(cellNum++);
                cell.setCellValue(StringUtils.defaultString(shoppingCartSituationVo.getStudentNames()));

                cell = row.createCell(cellNum++);
                cell.setCellValue(StringUtils.defaultString(shoppingCartSituationVo.getCustomerPhoneNo()));


            }
        }

        return excel;
    }

}